﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class GetCodigo
    {
        private String url;
        private String urlbio;

        public GetCodigo()
        {
            this.url = @"http://www.allaboutjazz.com/php/musician_center.php";
            this.urlbio = @"http://www.allaboutjazz.com/php/musician.php?id=";
        }

        public GetCodigo(String arg)
        {
            this.url = arg;
        }

        public void getFonte()
        {
            string arquivo = @"Data.txt";
            TextWriter ficheiro = new StreamWriter(arquivo);
            StringBuilder sb = new StringBuilder();

            byte[] buf = new byte[8192];

            // ligaçao
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.url);

            // executa o pedido
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                count = resStream.Read(buf, 0, buf.Length);

                if (count != 0)
                {
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    sb.Append(tempString);
                }
            }
            while (count > 0);
            
            ficheiro.Write(sb.ToString());
            ficheiro.WriteLine("DONE:!:!:!");
            ficheiro.Close();
        }

        public void getFonteBio(String id)
        {
            string arquivo = "" + id + ".txt";
            TextWriter ficheiro = new StreamWriter(arquivo);
            StringBuilder sb = new StringBuilder();

            byte[] buf = new byte[8192];

            // ligaçao
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.urlbio + "" + id);

            // executa o pedido
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                count = resStream.Read(buf, 0, buf.Length);

                if (count != 0)
                {
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    sb.Append(tempString);
                }
            }
            while (count > 0);

            ficheiro.Write(sb.ToString());
            ficheiro.Close();
        }
    }
}
